import { http } from "@/utils/http";

// 获取产品列表返回类型
export type ProductListResult = {
  code: number;
  msg: string;
  time: string;
  data: ProductListData;
};

// 产品列表data数据类型
export type ProductListData = {
  total: number;
  page: number;
  limit: number;
  list: ProductListDataItem[];
};

// 产品列表data数据类型
export type ProductListDataItem = {
  id: number;
  name: string;
  image: string;
  product_type: string;
  exhibitor_id: number;
  status: number;
};

// 获取产品列表
export const getProductList = (
  product_type: string,
  page = 1,
  limit = 10,
  exhibitor_id?: number,
  product_name
) => {
  return http.request<ProductListResult>("get", "/api/product/list", {
    params: {
      product_type,
      page,
      limit,
      exhibitor_id,
      product_name
    }
  });
};

// 申请产品
export const applyProduct = (data: any) => {
  return http.request("post", "/api/product/apply", {
    data
  });
};

// 获取产品详情
export const getProductDetail = (id: number, exhibitor_id?: number) => {
  return http.request("get", "/api/product/detail", {
    params: {
      id,
      exhibitor_id
    }
  });
};

// 更新产品
export const updateProduct = (data: any) => {
  return http.request("post", "/api/product/update", {
    data
  });
};

// 获取图片地址返回类型
export interface ImagePath {
  url: string;
  full_url: string;
}

// 上传图片
export const uploadImage = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);

  return http.request("post", "/api/common/upload", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data" // 覆盖全局 application/json
    }
    // 不手动设置Content-Type，让浏览器自动设置multipart/form-data和boundary
  });
};

// 更改产品状态
export const updateProductStatus = (
  id: number,
  exhibitor_id: number,
  status: number
) => {
  return http.request("post", "/api/product/changeStatus", {
    data: {
      id,
      exhibitor_id,
      status
    }
  });
};
